#include "PhysicsManager.hpp"
#include "../utils/Logger.hpp"
#include "../utils/Utils.hpp"

using namespace klasPhysics;

// This function adds the object to the list of 'alive' objects
void PhysicsManager::AddObject(Object *obj)
{
	m_AliveObjects.push_back(obj);
}

// Performs a quick collision test on the objects
void PhysicsManager::GetCollisions()
{
	for(list<Object*>::iterator it = m_AliveObjects.begin(); it != m_AliveObjects.end(); ++it)
		for(list<Object*>::iterator it2 = m_AliveObjects.begin(); it2 != m_AliveObjects.end(); ++it2)
			if(it2 != it)
				(*it)->FastCollisionTest(*it2);
}

void PhysicsManager::Update(Real totalTime, Real timeStep)
{
	GetCollisions();
	for(list<Object*>::iterator it = m_AliveObjects.begin(); it != m_AliveObjects.end(); ++it)
	{
		Logger::Log(GetString(((*it)->m_State.location.y)));
		(*it)->NormalCollisionTest();
		(*it)->AddForce(m_Gravity);
		(*it)->Integrate(totalTime, timeStep);
		
	}
}







// Set / get functions
void PhysicsManager::SetGravity(const Vector3D &gravity)
{
	m_Gravity = gravity;
}